!
$(function() {
    // tooltips hover
    $('[data-toggle=tooltip]').tooltip({
        container: "body",
        html: true
    })
    // tooltips click
    $('[data-toggle=popover]').popover({html: true});

    slieOperationFn();

    $("body").on("click", "#overall-select", function () {
        overallSelect();
    });
});

// sort more fun
var sortMoreFun = function() {
    //var _e = $('<aside class="sMoreBtn"><span class="fs16 c-999"><i class="fa fa-chevron-down" title="更多"></i></span></aside>')
    var _smFun = function(p) {
        var _f = false
        $('.sMoreBtn', p).on('click', function() {
            var _l = $(this)
            if(!_f) {
                _l.find('i').removeClass('fa-chevron-down').addClass('fa-chevron-up').attr('title', '收起')
                _l.prev('ul').css('height', 'auto')
                _f = true
            } else {
                _l.find('i').removeClass('fa-chevron-up').addClass('fa-chevron-down').attr('title', '更多')
                _l.prev('ul').css('height', '30px')
                _f = false
            }
        })
    }
    $('.cm-sort', '.cm-sort__wrap').each(function(i, n) {
        var _t = $(this),
            _d = _t.find('.cm-filter-item-list')
        _p = _d.parent('.media-body')

        if(_d.outerHeight(true) > 42) {
            _d.css('height', '30px').next('.sMoreBtn').show()
            _smFun(_p)
        }

    })
};

//目录菜单
var tchpterFun = function() {
    $('body').on('click', '.cm-task-chpter-item>.tchpter-item-parent', function() {
        var that = $(this), _tchild = that.parent().children('.tchpter-item-children');
        if(_tchild.length > 0) {
            if(!_tchild.is(':hidden')) {
                that.parent().removeClass('cOpen');
                _tchild.hide();
            } else {
                that.parent().addClass('cOpen');
                _tchild.show();
            }
        }
    })
};

//基本模态框
/**
 *   type: 类型：0，1，2；代表：msg，alert，confirm
 *   title: 弹层标题
 *   content: 文本内容
 *   btnTxt: 按钮文本，默认为 ‘确定’
 *   area: 宽高，默认为 ‘auto’
 *   callback: 按钮回调
 */
var icModale = function(type, title, content, btnTxt, area, callback) {
    switch(type) {
        case 0:
            layer.msg(content)
            break
        case 1:
            layer.open({
                type: 1,
                title: title ? title : '提示',
                content: content,
                btn: btnTxt ? btnTxt : ' 确 定 ',
                area: area ? area : 'auto',
                btnAlign: 'c',
                shade: [0.3, '#000000'],
                skin: 'lay_ppComment__wrap',
                yes: function(index, ele){
                    if($.isFunction(callback)) callback(index, ele)
                }
            });
            break;
        case 2:
            layer.open({
                type: 1,
                title: title ? title : '提示',
                content: content,
                btn: [btnTxt ? btnTxt : ' 确 定 ', ' 取 消 '],
                area: area ? area : 'auto',
                btnAlign: 'c',
                shade: [0.3, '#000000'],
                skin: 'lay_ppComment__wrap',
                yes: function(index, ele){
                    if($.isFunction(callback)) callback(index, ele)
                }
            })
            break
    }
};

//向上滚动方法
var upScroll = function (op) {
    var _upWrap = $(op),
        _mli = _upWrap.children("li"),
        _mliH = _mli.outerHeight(),
        _sTime = 3000,
        _moving = null;

    if(_mli.length * _mliH > _upWrap.outerHeight()) {
        _upWrap.hover(function() {
            clearInterval(_moving);
        }, function() {
            _moving = setInterval(function() {
                var _mC = _upWrap.find("li:first"),
                    _mH = _mC.outerHeight();
                _mC.stop().animate({"margin-top" : -_mH + "px"}, 600, function() {
                    _mC.css("margin-top", 0).appendTo(_upWrap);
                });
            }, _sTime);
        }).trigger("mouseleave");
    }

};

//返回顶部/反馈/下载
function slieOperationFn() {
    try{
        var sEle = '';
        sEle += '<aside id="js-slide-operation" class="slideOPeraction__wrap">';
        sEle +=     '<div class="mod-side-operation">';
        sEle +=         '<div class="js-jump__wrap"><span id="js-goTop-btn" class="mode-slide-operation__jump-goTop"><i class="fa fa-chevron-up fs24"></i><span class="item-hover-text">返回顶部</span></span></div>'; //返回顶部
        sEle +=         '<div class="js-jump__wrap ' + (appDownloadCode && h5DownloadCode && smallAppDownloadCode ? 'hide' : '') + '"><span id="js-download-btn" class="mode-slide-operation__download"><i class="fa fa-mobile-phone fs30"></i><span class="item-hover-text">扫码下载</span></span>'; //扫码下载
        sEle +=         '<span id="js-apcode-tips" class="mode-slide-operation__apcode"><img src="'+appDowMapUrl+'" width="70" height="70" alt="app扫码下载" class=" ' + (appDownloadCode ? 'hide' : '') + '"><p class="fs12 c-999 mb10 ' + (appDownloadCode ? 'hide' : '') + '">app扫码下载</p><img src="'+h5MobileMapUrl+'" width="70" height="70" alt="H5&微信扫码" class=" '+ (appDownloadCode ? 'hide' : '') + '"><p class="fs12 c-999 '+ (appDownloadCode ? 'hide' : '') + '">H5&微信扫码</p><img src="'+smallAppMapUrl+'" width="70" height="70" alt="小程序" class="'+(smallAppDownloadCode ? 'hide' : '') +'"><p class="fs12 c-999 '+(smallAppDownloadCode ? 'hide' : '') +'">小程序</p></span></div>'; //扫码下载
        sEle +=         '<div class="js-jump__wrap" onclick="window.open(\'\'+serviceUrl+\'\',\'newwindow\',\'toolbar=no,scrollbars=yes,location=no,resizable=no,top=200,left=300,width=800,height=600\')"><span id="js-comment-btn" class="mode-slide-operation__comment"><i class="fa fa-comments-o fs24"></i><span class="item-hover-text">在线咨询</span></span></div>'; //在线咨询
        sEle +=         '<div class="js-jump__wrap" onclick="window.open(\'/web/feedback/index\',\'_blank\')"><span id="js-feedback-btn" class="mode-slide-operation__feedback"><i class="fa fa-edit fs24"></i><span class="item-hover-text">意见反馈</span></span></div>'; //意见反馈
        sEle +=     '</div>';
        sEle += '</aside>';

        $('body').append(sEle)
        var goTopBtn = $('#js-goTop-btn'),
            op = function () {
                var sTop = document.documentElement.scrollTop + document.body.scrollTop;
                sTop > 80 ? goTopBtn.fadeIn(300).css('display', 'block') : goTopBtn.fadeOut(300)
            };
        $(window).on('scroll', op);

        goTopBtn.on('click', function () {
            $('html, body').animate({scrollTop: 0}, 500)
        });
    }catch (e){
        console.dir(e);
    }
}

/**
 * 获取专业数据
 * @param parentId
 */
function findSubjectList(callback) {
    $.ajax({
        url:baseloaction +'/web/subject/ajax/findLayeredSubjectList',
        type:'post',
        dataType:'json',
        async: false,
        success:function (result) {
            if(result.success==true){
                if($.isFunction(callback)){
                    callback(result.entity);
                }
            }else{
                icModale(0,'',result.message);
            }
        },
        error:function (error) {
            icModale(0,'','系统错误，专业数据获取失败');
        }
    });
}

/**
 * 获取所有讲师
 * @param callback 回调
 */
function findAllTeacherList(callback,teacherIdentity) {
    $.ajax({
        url:baseloaction +'/web/teacher/findAllTeacherList?teacherIdentity='+teacherIdentity,
        type:'post',
        dataType:'json',
        success:function (result) {
            if(result.success==true){
                if($.isFunction(callback)){
                    callback(result.entity);
                }
            }else{
                icModale(0,'',result.message);
            }
        },
        error:function (error) {
            icModale(0,'','系统错误，讲师数据获取失败');
        }
    });
}
/**
 * 初始化个人中心滚动条
 */
function ucCenterScroll() {
    var largeHeader = document.getElementById('uCenterMain');
    if(largeHeader==null||largeHeader=='undefined'||largeHeader.size<=0){
        largeHeader = document.getElementById('wrap');
    }
    if(largeHeader==null){
        return;
    }
    var height = window.innerHeight;
    largeHeader.style.height = height+'px';
    $('.u-c-directory').slimScroll({
        height: '100%',
        railOpacity: 0.9,
        alwaysVisible: false
    });
    /*$('.u-my-course_box').slimScroll({
        height: '100%',
        railOpacity: 0.9,
        alwaysVisible: true
    });*/
}

/**
 * 加载课程图片
 */
function lazyCourseImage() {
    //图片数据懒加载
    $('img.lazy').lazyload({
        placeholder: staticPath + '/front/web/v3/img/default/default-course.gif',  //占位图
        effect: 'fadeIn',//载入方式
        threshold: 200, // 提前开始加载
    });
}

/**
 * 加载讲师图片
 */
function lazyTeacherImage() {
    //图片数据懒加载
    $('img.lazy').lazyload({
        placeholder: staticPath + '/front/web/v3/img/default/default-face.gif',  //占位图
        effect: 'fadeIn' ,//载入方式
        threshold: 200, // 提前开始加载
    });
}

/**
 * 加载用户图像
 */
function lazyUserImage() {
    //图片数据懒加载
    $('img.lazy').lazyload({
        placeholder: staticPath + '/front/web/v3/img/default/default-face.gif',  //占位图
        effect: 'fadeIn' ,//载入方式
        threshold: 200, // 提前开始加载
    });
}

/**
 * 加载用户图像
 */
function lazyStudentImage() {
    //图片数据懒加载
    $('img.lazy-user').lazyload({
        placeholder: staticPath + '/front/web/v3/img/default/default-face.gif',  //占位图
        effect: 'fadeIn',//载入方式
        threshold: 200, // 提前开始加载
    });
}

/**
 * 设置图片懒加载，无占位图片
 */
function lazyImage() {
    //图片数据懒加载
    $('img.lazy').lazyload({
        effect: 'fadeIn',//载入方式
        threshold: 200, // 提前开始加载
    });
}
/**
 * 加载图书
 */
function lazyBookImage() {
    //图片数据懒加载
    $('img.lazy').lazyload({
        placeholder: staticPath + '/front/web/v3/img/default/default-lab.gif',  //占位图
        effect: 'fadeIn',//载入方式
        threshold: 200, // 提前开始加载
    });
}


function getParameter(name) {
    var url = document.location.href;
    var start = url.indexOf("?")+1;
    if (start==0) {
        return "";
    }
    var value = "";
    var queryString = url.substring(start);
    var paraNames = queryString.split("&");
    for (var i=0; i<paraNames.length; i++) {
        if (name==getParameterName(paraNames[i])) {
            value = getParameterValue(paraNames[i])
        }
    }
    return value;
}
function getParameterName(str) {
    var start = str.indexOf("=");
    if (start==-1) {
        return str;
    }
    return str.substring(0,start);
}

function getParameterValue(str) {
    var start = str.indexOf("=");
    if (start==-1) {
        return "";
    }
    return str.substring(start+1);
}

/**
 * 打开新标签的方法
 * @param url
 */
function openBlankWin(url) {
    var form = document.createElement('form');
    form.action = url;
    //form.style = 'display:none;'
    form.target = '_blank';
    document.body.appendChild(form);
    form.submit();
}

var LOCALSTORAGE = localStorage
var isEmpty = function (str) {
    if (str === null || typeof str === 'undefined' || str === '') {
        return true
    }
    return false
}
var _setLOCALSTORAGE = function (key, value) {
    if (isEmpty(key)) {
        return console.error('ReferenceError: key in set is not defined')
    }
    if (value instanceof Object) {
        value = JSON.stringify(value)
    }
    LOCALSTORAGE[key] = value
    return LOCALSTORAGE
}
var _getLOCALSTORAGE = function (key) {
    if (isEmpty(key)) {
        return console.error('ReferenceError: key in get is not defined')
    }
    var value = LOCALSTORAGE[key]
    try {
        value = JSON.parse(value)
    } catch (error) {}
    return value
}
var _removeLOCALSTORAGE = function (key) {
    if (isEmpty(key)) {
        return LOCALSTORAGE.clear()
    } else {
        return LOCALSTORAGE.removeItem(key)
    }
}


function overallSelect() {
    var overallValue = _getLOCALSTORAGE("overallKey");
    var overallName = $("#overallName").val();
    if (isEmpty(overallValue)) {
        overallValue = [overallName];
        _setLOCALSTORAGE("overallKey", overallValue);
    } else {
        var index = overallValue.indexOf("" + overallName + "");
        if (index <= -1 && !isEmpty(overallName)) {
            overallValue.push(overallName);
            _setLOCALSTORAGE("overallKey", overallValue);
        }
    }
    httpPost(baseloaction + "/web/globalSearch/globalSearchIndex", {"overallValue": overallValue,"overallName":overallName});
}

//发送POST请求跳转到指定页面
function httpPost(URL, PARAMS) {
    var temp = document.createElement("form");
    temp.action = URL;
    temp.method = "post";
    temp.style.display = "none";
    for (var x in PARAMS) {
        var opt = document.createElement("textarea");
        opt.name = x;
        opt.value = PARAMS[x];
        temp.appendChild(opt);
    }
    document.body.appendChild(temp);
    temp.submit();
    return temp;
}




